package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wolfcode on 2018/8/17.
 */
@Controller
public class PropertyController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyService propertyService;
    @RequestMapping("/property")
    public String propertyPage(Model model){
        model.addAttribute("allCatalog",catalogService.selectAllFromCache());
        return "property/property";
    }
    @RequestMapping("/property/get/{catalogId}")
    public String propertyListPage(@PathVariable("catalogId") Long catalogId,Model model){
        model.addAttribute("propertyList",propertyService.queryByCatalogId(catalogId));
        return "property/property_list";
    }
    @RequestMapping("property/save")
    @ResponseBody
    public JSONResultVo saveOrUpdata(Property property){
        JSONResultVo vo = new JSONResultVo();
        try{
            propertyService.saveOrUpdate(property);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
    @RequestMapping("property/delete/{propertyId}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("propertyId") Long propertyId){
        JSONResultVo vo = new JSONResultVo();
        try{
            propertyService.delete(propertyId);
        }catch(Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
}
